<?php
/**
 * Shortcodes bundled for use with the theme. These shortcodes can be used in any shortcode-ready
 * area, which includes posts, pages, and various dkret3 theme areas including the footer and
 * every widget.
 *
 * @uses add_shortcode() to create new shortcodes.
 * @link http://codex.wordpress.org/Shortcode_API
 *
 * @package dkret3
 */

if ( ! function_exists( 'dkret_entry_title' ) ) :
/**
 * Default entry title.  Shows the calendar and the title of each post. 
 * dkret_entry_title filter to customize.
 *
 * @since 3.6
 */
function dkret_entry_title( $metadata = '' ) {

	if ( empty( $metadata ) )
		$metadata = '[entry-calendar] [entry-title]';
  
	echo do_shortcode( apply_filters( 'dkret_entry_title', $metadata ) );
}
endif;

if ( ! function_exists( 'dkret_entry_meta_before' ) ) :
/**
 * Default entry meta information before each posts.  Shows author, date, and edit link.  Use the 
 * dkret_entry_meta_before filter to customize.
 *
 * @since 3.6
 */
function dkret_entry_meta_before( $metadata = '' ) {

	if ( empty( $metadata ) )
        $metadata = ''. __('<span class="entry-meta-before-prep entry-meta-before-prep-author">By</span> [entry-author] <span class="entry-meta-before-prep entry-meta-before-prep-published">on</span> [entry-published]', 'dkret' ) . '';
        
	$metadata = '<p class="entry-meta-before">' . $metadata . '</p>';

	echo do_shortcode( apply_filters( 'dkret_entry_meta_before', $metadata ) );
}
endif;

if ( ! function_exists( 'dkret_entry_meta' ) ) :
/**
 * Displays the default entry metadata.  Shows the category, tag, and comments 
 * link.  Use the dkret_entry_meta filter to customize.
 *
 * @since 3.6
 */
function dkret_entry_meta( $metadata = '' ) {
	global $post;

    if ( is_attachment() )
        return;
    
    if ( $metadata ) :
        $metadata = '<p class="entry-meta">' . $metadata . '</p>';
  
    elseif ( empty ( $metadata ) ) :

        if ( 'post' == $post->post_type && 'link_category' !== get_query_var( 'taxonomy' ) ) :
            $metadata = '<p class="entry-meta post-meta-data">[entry-categories] [entry-tags] [entry-comments] [entry-edit-link]</p>';
               
        elseif ( is_page() && current_user_can( 'edit_pages' ) ) :

            $metadata = '<p class="entry-meta post-meta-data">';
            $metadata .= '<span class="edit"><a class="post-edit-link edit-link" href="' . get_edit_post_link( $post->ID ) . '" title="' . __( 'Edit page', 'dkret' ) . '">' . __( 'Edit', 'dkret' ) . '</a></span>';
            $metadata .= '</p>';
        endif;
        
    endif;
    
    echo do_shortcode( apply_filters( 'dkret_entry_meta', $metadata ) ); // Filter to override default dkret3 .entry-meta
}
endif;

/**
 * Function for displaying a comment's metadata.
 *
 * @since 3.6
 */
function dkret_comment_meta( $metadata = '' ) {
	global $comment, $post;

	if ( empty ( $metadata ) )
		$metadata = '[comment-author] [comment-published] [separator] [comment-permalink] [separator] [comment-reply-link] [comment-edit-link]';
  
	$metadata = '<div class="comment-meta comment-meta-data">' . $metadata . '</div>';

	echo do_shortcode( apply_filters( 'dkret_comment_meta', $metadata ) );
}

if ( ! function_exists( 'dkret_footer_text' ) ) :
/**
 * Display the default Footer Text. 
 * Use dkret_footer_text filter to customize.
 *
 * @since 3.6
 */
function dkret_footer_text( $text = '' ) {

    $text = get_dkret_option( 'footer_text' );

    if ( empty( $text ) )
		$text = '<span class="copyright">' . __( 'Copyright &#169; [the-year] [site-link].', 'dkret' ) . '</span>' . " " . '<span class="credit">' . __( 'Powered by [wp-link] and [theme-link].', 'dkret' ) . '</span> [separator] [login-link]';
  
	stripslashes( $text );

	echo do_shortcode( apply_filters( 'dkret_footer_text', $text ) );
}
endif; 

if ( ! function_exists( 'dkret_adsense_shortcode' ) ) :
/**
 * Add AdSense Code Blocks in the the dkret3 settings Page.
 * Use [adsense] inside a post to display left aligned AdSense Block 3 in posts.
 * To change alignment use [adsense align="alignright"] or [adsense align="aligncenter"]
 * You can also set the ID options to display one of the other Blocks [adsense ID="1"]
 *
 * @since 3.0
 */
function dkret_adsense_shortcode( $atts ) {

    extract( shortcode_atts( array( 'align' => 'alignleft', 'ID' => '3' ), $atts ) );

    $google_adsense = "<div class=\"$align adsense adsense_block_$ID\">\n";
    $google_adsense .= get_option( "dkret_adsense_block_$ID" );
    $google_adsense .= "</div>\n";

    if ( is_home() || ! get_option( "dkret_adsense_block_$ID" ) )
        return;
    if ( is_user_logged_in() && get_dkret_option( 'hide_adsense' ) ) 
        return;

    return $google_adsense;
}
endif;

if ( ! function_exists( 'dkret_subscribe_rss_shortcode' ) ) :
/**
 * Use [rss] shortcode to display a subscribe link to your posts and pages
 * 
 * @since 3.0
 */
function dkret_subscribe_rss_shortcode() {
    $subscribe = '<div class="rss-box"><a href="'. get_bloginfo( 'rss2_url' ) .'" title="'. __( 'Enjoyed this post? Subscribe to my RSS feed!', 'dkret' ) .'">'. __( 'Subscribe', 'dkret' ) .'</a></div>';
    return $subscribe;
}
endif;

if ( ! function_exists( 'dkret_subscribe_comments_shortcode' ) ) :
/**
 * Use [subscribe] shortcode in post to subscribe the posts rss feed
 * 
 * @since 3.5
 */
function dkret_subscribe_comments_shortcode() {
    $subscribe = '<div class="rss-box"><a href="'. get_post_comments_feed_link() .'" title="'. __( 'Enjoyed this post? Subscribe to my RSS feed!', 'dkret' ) .'">'. __( 'Subscribe', 'dkret' ) .'</a></div>';
    return $subscribe;
}
endif;

if ( ! function_exists( 'dkret_twitter_shortcode' ) ) :
/**
 * Use [twitter] shortcode in post to share your post on twitter.com
 * 
 * @since 3.0
 */
function dkret_twitter_shortcode() {
    $twitter = '<div class="twitter"><a href="http://twitter.com/home?status=Currently reading '. get_permalink( $post->ID ). '" title="'. __( 'Share on Twitter!', 'dkret' ) .'" target="_blank">'. __( 'Share on Twitter', 'dkret' ) .'</a></div>';
    return apply_filters( 'dkret_twitter', $twitter );
}
endif;

if ( ! function_exists( 'dkret_donate_shortcode' ) ) :
/**
 * Use [donate text="Make a donation" account="your@paypal-account-email.com" purpose="Whatever"] shortcode in post
 * to display a paypal donation link. If you don't set the 'purpose' option the post-titel is used  
 * 
 * @since 3.0
 */
function dkret_donate_shortcode( $atts ) {
    extract( shortcode_atts( array( 'text' => 'Make a donation', 'account' => 'joern@diekretzschmars.de', 'purpose' => '' ), $atts ) );

    if ( ! $purpose ) $purpose = str_replace( " ", "+", get_the_title() );

    $donate = '<a class="donation-link" href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business='. $account .'&item_name=Donation+for+'. $purpose .'">'. $text .'</a>';
    return $donate;
}
endif;

if ( ! function_exists( 'dkret_separator_shortcode' ) ) :
/**
 * The [separator] shortcode adds the 'separator' html code used in other parts of dkret3.
 *
 * @since 3.0
 */
function dkret_separator_shortcode() {
    return '<span class="separator">|</span>';
}
endif;


// comment-specific shortcodes

if ( ! function_exists( 'dkret_comment_author' ) ) :
/**
 * Function for displaying a comment's author.
 *
 * @since 3.6
 */
function dkret_comment_author_shortcode( $author = '' ) {
    global $comment;
    $author = '<div class="comment-author vcard">';
    
    $dkret_avatar_size = apply_filters( 'dkret_avatar_size', '40' ); // Filter for changing avatar size
    $author .= get_avatar( $comment, $dkret_avatar_size );
    $author .= get_comment_author_link();

    $author .= '</div>';
    
    return $author;
}
endif;

if ( ! function_exists( 'dkret_comment_date_link_shortcode' ) ) :
/**
 * Displays the published date and time of an individual comment.
 *
 * @since 3.6
 */
function dkret_comment_date_link_shortcode( $attr ) {
	
    $attr = shortcode_atts( array( 'before' => '', 'after' => '' ), $attr );

	$link = '<span class="published">' . sprintf( __( '%1$s at %2$s', 'dkret' ), '<abbr class="comment-date" title="' . get_comment_date( __( 'l, F jS, Y, g:i a', 'dkret' ) ) . '">' . get_comment_date() . '</abbr>', '<abbr class="comment-time" title="' . get_comment_date( __( 'l, F jS, Y, g:i a', 'dkret' ) ) . '">' . get_comment_time() . '</abbr>' ) . '</span>';
	return $attr['before'] . $link . $attr['after'];
}
endif;

if ( ! function_exists( 'dkret_comment_permalink_shortcode' ) ) :
/**
 * Displays the permalink to an individual comment.
 *
 * @since 3.6
 */
function dkret_comment_permalink_shortcode( $attr ) {
    global $comment;
    
	$attr = shortcode_atts( array( 'before' => '', 'after' => '' ), $attr );

	$link = '<a class="permalink" href="' . get_comment_link( $comment->comment_ID ) . '" title="' . sprintf( __( 'Permalink to comment %1$s', 'dkret' ), $comment->comment_ID ) . '">' . __( 'Permalink', 'dkret' ) . '</a>';
	return $attr['before'] . $link . $attr['after'];
}
endif;

if ( ! function_exists( 'dkret_comment_edit_link_shortcode' ) ) :
/**
 * Displays a comment's edit link to users that have the capability to edit the comment.
 *
 * @since 3.6
 */
function dkret_comment_edit_link_shortcode( $attr ) {
	global $comment;

	$attr = shortcode_atts( array( 'before' => '', 'after' => '' ), $attr );
    
	$edit_link = get_edit_comment_link( $comment->comment_ID );

	if ( ! $edit_link )
		return '';

	$link = '<a class="comment-edit-link edit-link" href="' . $edit_link . '" title="' . __( 'Edit comment', 'dkret' ) . '"><span class="edit">' . __( 'Edit', 'dkret' ) . '</span></a>';
	$link = apply_filters( 'edit_comment_link', $link, $comment->comment_ID );

	return $attr['before'] . $link . $attr['after'];
}
endif;

if ( ! function_exists( 'dkret_comment_reply_link_shortcode' ) ) :
/**
 * Displays a reply link for the 'comment' comment_type if threaded comments are enabled.
 *
 * @since 0.7
 */
function dkret_comment_reply_link_shortcode( $attr ) {

	if ( !get_option( 'thread_comments' ) || 'comment' !== get_comment_type() )
		return '';

	$defaults = array(
		'reply_text' => __( 'Reply', 'dkret' ),
		'login_text' => __( 'Log in to reply.', 'dkret' ),
		'depth' => $GLOBALS['comment_depth'],
		'max_depth' => get_option( 'thread_comments_depth' ),
		'before' => '',
		'after' => ''
	);
	$attr = shortcode_atts( $defaults, $attr );

	return get_comment_reply_link( $attr );
}
endif;

// entry-specific shortcodes

if ( ! function_exists( 'dkret_entry_calendar_shortcode' ) ) :
/**
 * Function to print the calendar dates for the blog home
 *
 * @since 3.6
 */
function dkret_entry_calendar_shortcode() {
    if ( is_home() && !is_archive() )
        return sprintf( '<div class="entry-calendar-date">%1$s<span class="entry-calendar-day">%2$s</span><span class="entry-calendar-year">%3$s</span></div>',
                    get_the_time( 'M' ),
                    get_the_time( 'd' ),
                    get_the_time( 'Y' ) );
}
endif;

if ( ! function_exists( 'dkret_entry_title_shortcode' ) ) :
/**
 * Function to print the title
 *
 * @global $post
 * @since 3.6
 */
function dkret_entry_title_shortcode() {
    global $post;

    if ( is_front_page() && !is_home() )
        $title = the_title( '<h2 class="page-title entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>', false );

    elseif ( is_attachment() )
        $title = sprintf( '<h2 class="' . $post->post_type . '-title entry-title"><a href="%1$s" title="%2$s" rel="bookmark">&laquo; %3$s</a></h2>',
				get_permalink( $post->post_parent ),
				sprintf( __( 'Return to %s', 'dkret' ), esc_html( get_the_title( $post->post_parent ), 1 ) ),
				get_the_title( $post->post_parent ) );

    elseif ( is_single() )
        $title = the_title( '<h2 class="' . $post->post_type . '-title single-title entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>', false );

    else
        $title = the_title( '<h2 class="entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>', false );

    return $title;
}
endif;

if ( ! function_exists( 'dkret_entry_author_shortcode' ) ) :
/**
 * Displays an individual post's author with a link to his or her archive.
 *
 * @since 3.6
 * @param array $attr
 */
function dkret_entry_author_shortcode( $attr ) {
	$attr = shortcode_atts( array( 'before' => '', 'after' => '' ), $attr );
	$author = '<span class="author vcard"><a class="url fn n" href="' . get_author_posts_url( get_the_author_meta( 'ID' ) ) . '" title="' . get_the_author_meta( 'display_name' ) . '">' . get_the_author_meta( 'display_name' ) . '</a></span>';
	return $attr['before'] . $author . $attr['after'];
}
endif;

if ( ! function_exists( 'dkret_entry_published_shortcode' ) ) :
/**
 * Displays the published date of an individual post.
 *
 * @since 3.6
 * @param array $attr
 */
function dkret_entry_published_shortcode( $attr ) {
	$attr = shortcode_atts( array( 'before' => '', 'after' => '', 'format' => get_option( 'date_format' ) ), $attr );

	$published = '<abbr class="published" title="' . sprintf( get_the_time( __( 'l, F jS, Y, g:i a', 'dkret' ) ) ) . '">' . sprintf( get_the_time( $attr['format'] ) ) . '</abbr>';
	return $attr['before'] . $published . $attr['after'];
}
endif;

if ( ! function_exists( 'dkret_entry_edit_link_shortcode' ) ) :
/**
 * Function to print the edit link of each entry
 *
 * @since 3.6
 */
function dkret_entry_edit_link_shortcode( $attr ) {
    global $post;

    $attr = shortcode_atts( array( 'before' => '', 'after' => '' ), $attr );
    
    if ( current_user_can( 'edit_posts' ) && 'post' == $post->post_type && 'link_category' !== get_query_var( 'taxonomy' ) )
        $link = sprintf( '<a class="post-edit-link edit-link" href="' . get_edit_post_link( $post->ID ) . '" title="' . __( 'Edit post', 'dkret' ) . '">' . __( 'Edit', 'dkret' ) . '</a>' );

	return $attr['before'] . $link . $attr['after'];
}
endif;

if ( ! function_exists( 'dkret_entry_terms_shortcode' ) ) :
/**
 * Displays a list of terms for a specific taxonomy.
 *
 * Example to show categories:
 * [entry-terms taxonomy="category" before="' . __( 'Posted in', 'dkret' ) . ' "] 
 *
 * @since 3.6
 * @param array $attr
 */
function dkret_entry_terms_shortcode( $attr ) {
	global $post;

	$attr = shortcode_atts( array( 'id' => $post->ID, 'taxonomy' => 'post_tag', 'separator' => ', ', 'before' => '', 'after' => '' ), $attr );

	return '<span class="' . $attr['taxonomy'] . '">' . get_the_term_list( $attr['id'], $attr['taxonomy'], $attr['before'], $attr['separator'], $attr['after'] ) . '</span>';
}
endif;

if ( ! function_exists( 'dkret_entry_categories_shortcode' ) ) :
/**
 * Function to print the categories link of each entry
 *
 * @since 3.6
 */
function dkret_entry_categories_shortcode( $attr ) {
    global $post;

    $attr = shortcode_atts( array( 'before' => '', 'after' => '' ), $attr );
    
    $categories = get_the_term_list( $post->ID, 'category', '<span class="categories"><span class="meta-prep meta-prep-categories">' . __( 'Posted in', 'dkret' ) . '</span> ', ', ', '</span>' );

    if ( $categories )
        return $attr['before'] . $categories . $attr['after'];
}
endif;

if ( ! function_exists( 'dkret_entry_tags_shortcode' ) ) :
/**
 * Function to print the tags link of each entry
 *
 * @since 3.6
 */
function dkret_entry_tags_shortcode( $attr ) {
    global $post;

    $attr = shortcode_atts( array( 'before' => '', 'after' => '' ), $attr );
    
    $tags = get_the_term_list( $post->ID, 'post_tag', '<span class="post_tag"><span class="meta-prep meta-prep-tags">' . __( 'Tagged', 'dkret' ) . '</span> ', ', ', '</span>' );

    if ( $tags )
        return $attr['before'] . $tags . $attr['after'];
}
endif;

if ( ! function_exists( 'dkret_entry_comments_shortcode' ) ) :
/**
 * Function to print the comments link of each entry
 *
 * @since 3.6
 */
function dkret_entry_comments_shortcode( $attr ) {
    global $post;

    $attr = shortcode_atts( array( 'before' => '', 'after' => '' ), $attr );
    
    if ( !is_singular() && comments_open() && 'page' !== $post->post_type ) :
        $number = get_comments_number();
        if ( $number == 0 )
            $comments_link = '<a class="comments-link" href="' . get_permalink() . '#respond" title="' . sprintf( __( 'Comment on %1$s', 'dkret' ), the_title_attribute( 'echo=0' ) ) . '">' . __( 'Leave a Comment', 'dkret' ) . '</a>';
        elseif ( $number == 1 )
            $comments_link = '<a class="comments-link" href="' . get_comments_link() . '" title="' . sprintf( __( 'Comment on %1$s', 'dkret' ), the_title_attribute( 'echo=0' ) ) . '">' . __( '1 Comment', 'dkret' ) . '</a>';
        else
            $comments_link = '<a class="comments-link" href="' . get_comments_link() . '" title="' . sprintf( __( 'Comment on %1$s', 'dkret' ), the_title_attribute( 'echo=0' ) ) . '">' . sprintf( __( '%1$s Comments', 'dkret' ), $number ) . '</a>';
    endif;

    if ( isset( $comments_link ) )
        return $attr['before'] . $comments_link . $attr['after'];
}
endif;

// theme-specific shortcodes

if ( ! function_exists( 'dkret_theme_link_shortcode' ) ) :
/**
 * The [theme-link] shortcode displays the link to dkret3 theme page
 *
 * @since 3.0
 */
function dkret_theme_link_shortcode() {
	$data = get_theme_data( TEMPLATEPATH . '/style.css' );
	return '<a class="theme-link" href="' . esc_url( $data['URI'] ) . '" title="' . esc_attr( $data['Name'] ) . ' ' . esc_attr( $data['Version'] ) . '"><span>' . esc_attr( $data['Name'] ) . '</span></a>';
}
endif;

if ( ! function_exists( 'dkret_child_theme_link_shortcode' ) ) :
/**
 * The [child-theme-link] shortcode displays the link to the child theme's page
 *
 * @since 3.0
 */
function dkret_child_theme_link_shortcode() {
	$data = get_theme_data( STYLESHEETPATH . '/style.css' );
	return '<a class="child-link" href="' . esc_url( $data['URI'] ) . '" title="' . esc_attr( $data['Name'] ) . ' ' . esc_attr( $data['Version'] ) . '"><span>' . esc_attr( $data['Name'] ) . '</span></a>';
}
endif;

if ( ! function_exists( 'dkret_copyright_shortcode' ) ) :
/**
 * The [copyright] shortcode displays copyright information, the actual year and a link to the blog home
 *
 * @since 3.0
 */
function dkret_copyright_shortcode() {
    $copyright = sprintf( '<span class="copyright"><span class="text">%1$s</span> <span class="the-year">%2$s</span> <a class="blog-title" href="%3$s" title="%4$s">%4$s</a></span>',
                            __( 'Copyright &copy;', 'dkret' ),
                            date( 'Y' ),
                            get_bloginfo( 'url' ),
                            get_bloginfo( 'name' ) );
    return $copyright;
}
endif;

if ( ! function_exists( 'dkret_wp_link_shortcode' ) ) :
/**
 * The [wp-link] shortcode adds a link to WordPress
 *
 * @since 3.0
 */
 function dkret_wp_link_shortcode() {
    return '<a class="wp-link" href="http://wordpress.org/" title="' . __( 'Powered by WordPress', 'dkret' ) . '" rel="generator">' . __( 'WordPress', 'dkret' ) . '</a>';
}
endif;

if ( ! function_exists( 'dkret_login_link_shortcode' ) ) :
/**
 * The [login-link] shortcode adds a login link
 *
 * @since 3.6
 */
function dkret_login_link_shortcode() {
    if ( is_user_logged_in() )
        $out = sprintf('<a class="login-link" href="%1$s">%2$s</a>', admin_url(), __('Site Admin') );
    else 
        $out = sprintf('<a class="login-link" href="%1$s">%2$s</a>', wp_login_url( esc_url( $_SERVER['HTTP_REFERER'] ) ), __('Log in') );

	return $out;
}
endif;

if ( ! function_exists( 'dkret_query_counter_shortcode' ) ) :
/**
 * Displays query count and load time if the current user can edit themes.
 *
 * @since 3.6
 */
function dkret_query_counter_shortcode() {
	if ( current_user_can( 'edit_themes' ) )
		$out = sprintf( __( 'This page loaded in %1$s seconds with %2$s database queries.', 'dkret' ), timer_stop( 0, 3 ), get_num_queries() );
	return $out;
}
endif;

if ( ! function_exists( 'dkret_site_link_shortcode' ) ) :
/**
 * The [site-link] shortcode adds the site title
 *
 * @since 3.0
 */
 function dkret_site_link_shortcode() {
	return '<a class="site-link" href="' . get_bloginfo( 'url' ) . '" title="' . get_bloginfo( 'name' ) . '" rel="home"><span>' . get_bloginfo( 'name' ) . '</span></a>';
}
endif;

if ( ! function_exists( 'dkret_the_year_shortcode' ) ) :
/**
 * The [the-year] shortcode adds the actual year.
 *
 * @since 3.0
 */
 function dkret_year_shortcode() {
    return '<span class="the-year">' . date( __( 'Y', 'dkret') ) . '</span>';
}
endif;

if ( ! function_exists( 'dkret_check_html_shortcode' ) ) :
/**
 * The [check-html] shortcode adds a xhtml validation link
 *
 * @since 3.6
 */
function dkret_check_html_shortcode() {
    $check_html = '<span class="html-validator-link"><a href="http://validator.w3.org/check?uri=referer" title="valid">HTML</a></span>';
    return $check_html;
}
endif;

if ( ! function_exists( 'dkret_check_rss_shortcode' ) ) :
/**
 * The [check-css] shortcode adds a css validation link
 *
 * @since 3.6
 */
function dkret_check_css_shortcode() {   
    $check_css = '<span class="css-validator-link"><a href="http://jigsaw.w3.org/css-validator/check/referer" title="valid">CSS</a></span>';
    return $check_css;
}
endif;

if ( ! function_exists( 'dkret_theme_version_shortcode' ) ) :
/**
 * The [theme-version] version of the dkret3 theme 
 *
 * @since 3.0
 */
function dkret_theme_version_shortcode() {
	$data = get_theme_data( TEMPLATEPATH . '/style.css' );
    return esc_attr( $data['Version'] );
}
endif;

if ( ! function_exists( 'dkret_child_theme_version_shortcode' ) ) :
/**
 * The [child-version] version of the child theme 
 *
 * @since 3.0
 */
function dkret_child_theme_version_shortcode() {
	$data = get_theme_data( STYLESHEETPATH . '/style.css' );
    return esc_attr( $data['Version'] );
}
endif;
?>